#include <WiFi.h>                                                                       // This library contains the Wifi function to run on ESP-32

#define LEDC_CHANNEL_0     0                                                            // In these 4 lines we have defined the PWM details for LED
#define LEDC_TIMER_13_BIT  13
#define LEDC_BASE_FREQ     5000
#define LED_PIN            21

#define min(a,b) ((a)<(b)?(a):(b));                                                     // Defining Min Function

const char* ssid     = "electronicsforu";
const char* password = "efygroup";

WiFiServer server(80);

#include "soc/soc.h"                                                                    // Use this line only if your ESP-32 has Brownout Error
#include "soc/rtc_cntl_reg.h"                                                           // Use this line only if your ESP-32 has Brownout Error

int brightness = 0;

void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255)          // function for analogWrite in Arduino 
{
  uint32_t duty = (8191 / valueMax) * min(value, valueMax);
  ledcWrite(channel, duty);
}

void setup()
{
    Serial.begin(115200);                                                               //Baud Rate of ESP-32
    delay(10);
    WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);                                          // Use this line only if your ESP-32 has Brownout Errorc
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) 
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    server.begin();                                                                     // Start the server
    ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);                       // PWM setup
    ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
}
int value = 0;
void loop(){
 WiFiClient client = server.available();                                                // listen for any incoming client

  if (client) {                                                                         // if you get any client,
    Serial.println("New Client.");                                                      // print a message on the serial monitor
    String currentLine = "";                                                            // make a String to hold incoming data from the client
    while (client.connected())                                                          // loop while the client's connected
    {                                                        
      if (client.available())                                                           // if there's bytes to read from the client
      {                                                         
        char c = client.read();                                                         // read a byte, then
        Serial.write(c);                                                                // print it out on the serial monitor
        if (c == '\n')                                                                  // if the byte is a newline character
        {                                                                
          if (currentLine.length() == 0)                                                // if the current line is blank, you got two newline characters in a row.
                                                                                        // that's the end of the client HTTP request, so send a response:
          { 
            client.println("HTTP/1.1 200 OK");                                          // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            client.println("Content-type:text/html");                                   // and a content-type so the client knows what's coming,
            client.println();                                                           // then a blank line:
                                                                                        // the content of the HTTP response follows the header:
            client.print("<a href=\"/A\">OFF</a><br>");
            client.print("<a href=\"/B\">Level 1</a><br>");
            client.print("<a href=\"/C\">Level 2</a><br>");
            client.print("<a href=\"/D\">Level 3</a><br>");
            client.print("<a href=\"/E\">Level 4</a><br>");
            client.print("<a href=\"/F\">Level 5 Full</a><br>");
            client.println();                                                            // The HTTP response ends with another blank line:
            break;                                                                       // break out of the while loop: 
          } 
          else 
          {    
            currentLine = "";                                                            // if you got a newline, then clear currentLine:
          }
        } 
        else if (c != '\r')                                                              // if you got anything else but a carriage return character,
        {  
          currentLine += c;                                                              // add it to the end of the currentLine
        }
        if (currentLine.endsWith("GET /A"))                                              // Check to see if the client request was "GET /A" or "GET /B" and so on...:
        {
          brightness = 0;
        }
        if (currentLine.endsWith("GET /B")) 
        {
          brightness = 50;
        }
        if (currentLine.endsWith("GET /C")) 
        {
          brightness = 100;
        }
        if (currentLine.endsWith("GET /D")) 
        {
          brightness = 150;
        }
        if (currentLine.endsWith("GET /E")) 
        {
          brightness = 200;
        }
        if (currentLine.endsWith("GET /F")) 
        {
          brightness = 255;
        }
        ledcAnalogWrite(LEDC_CHANNEL_0, brightness);                                       // the analog value of brightness is written on the pin
      }
    }
    client.stop();                                                                         // close the connection:
    Serial.println("Client Disconnected.");
  }
}